package net.midnighttd.engine;

import org.w3c.dom.*;
import org.xml.sax.*;
import javax.imageio.ImageIO;
import javax.xml.parsers.*;
import java.io.*;
import java.awt.image.BufferedImage;
import java.awt.*;
import java.util.HashMap;

/**
 * Author: Adam Scarr
 * Date: 22/04/2009
 * Time: 1:52:27 PM
 */
public class ResourceManager {
    private static ResourceManager instance;
    private static HashMap<String, Image> imageCache = new HashMap<String, Image>();
    private static HashMap<String, Element> xmlCache = new HashMap<String, Element>();
    private static String lastLoaded = "";

    protected ResourceManager() {
        
    }

    public static String getLastLoaded() {
        return lastLoaded;
    }

    public static synchronized ResourceManager getInstance() {
        if(instance == null) {
            instance = new ResourceManager();
        }

        return instance;
    }
    
    public static synchronized void setInstance(ResourceManager override) {
        instance = override;
    }

    public static InputStream get(String filename) {
        System.out.println("Loading external resource: " + filename);
        lastLoaded = filename;
        return getInstance().getResource(filename);
    }

    public static Image getImage(String filename) {
        Image img = imageCache.get(filename);
        if(img == null) {
            try {
                 img = ImageIO.read(get(filename));
            } catch (IOException e) {
                throw new Error("Could not get image '" + filename + "'. Does it exist?");
            }

            imageCache.put(filename, img);
        }

        return img;
    }

    public static BufferedImage getBufferedImage(String filename, int type) {
        Image i = getImage(filename);
        BufferedImage img = new BufferedImage(i.getWidth(null), i.getHeight(null), type);
        img.getGraphics().drawImage(i, 0, 0, null);

        return img;
    }

    public InputStream getResource(String filename) {
        try {
            return new FileInputStream(filename);
        } catch (FileNotFoundException e) {
            throw new Error("Unable to find file " + filename);
        }
    }

    public static Element getXML(String filename) {
        Element result = xmlCache.get(filename);

        if(result == null) {
            try {
                DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
                Document doc = docBuilder.parse (ResourceManager.get(filename));
                result = doc.getDocumentElement();
                result.normalize();
                xmlCache.put(filename, result);
            } catch (SAXParseException err) {
                System.out.println ("** Parsing error" + ", line "
                     + err.getLineNumber () + ", uri " + err.getSystemId ());
                System.out.println(" " + err.getMessage ());

            } catch (SAXException e) {
                Exception x = e.getException ();
                ((x == null) ? e : x).printStackTrace ();

            } catch (Throwable t) {
                t.printStackTrace ();
            }

        }

        return result;
    }
}
